package com.ruoyi.report_sn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sn.domain.bo.SnmdxdjcjlbBsInfoBo;
import com.ruoyi.report_sn.domain.vo.SnmdxdjcjlbBsInfoVo;
import com.ruoyi.report_sn.domain.SnmdxdjcjlbBsInfo;
import com.ruoyi.report_sn.mapper.SnmdxdjcjlbBsInfoMapper;
import com.ruoyi.report_sn.service.ISnmdxdjcjlbBsInfoService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 水泥密度、细度试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-06-06
 */
@RequiredArgsConstructor
@Service
public class SnmdxdjcjlbBsInfoServiceImpl implements ISnmdxdjcjlbBsInfoService {

    private final SnmdxdjcjlbBsInfoMapper baseMapper;

    private final IBiQualificationService biQualificationService;

    /**
     * 查询水泥密度、细度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public SnmdxdjcjlbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询水泥密度、细度试验检测记录列表
     */
    @Override
    public TableDataInfo<SnmdxdjcjlbBsInfoVo> queryPageList(SnmdxdjcjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnmdxdjcjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<SnmdxdjcjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥密度、细度试验检测记录列表
     */
    @Override
    public List<SnmdxdjcjlbBsInfoVo> queryList(SnmdxdjcjlbBsInfoBo bo) {
        LambdaQueryWrapper<SnmdxdjcjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnmdxdjcjlbBsInfo> buildQueryWrapper(SnmdxdjcjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnmdxdjcjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, SnmdxdjcjlbBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, SnmdxdjcjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SnmdxdjcjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), SnmdxdjcjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SnmdxdjcjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SnmdxdjcjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SnmdxdjcjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SnmdxdjcjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SnmdxdjcjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SnmdxdjcjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, SnmdxdjcjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), SnmdxdjcjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SnmdxdjcjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SnmdxdjcjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SnmdxdjcjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SnmdxdjcjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SnmdxdjcjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getMidsyzl1() != null, SnmdxdjcjlbBsInfo::getMidsyzl1, bo.getMidsyzl1());
        lqw.eq(bo.getMiddyc1() != null, SnmdxdjcjlbBsInfo::getMiddyc1, bo.getMiddyc1());
        lqw.eq(bo.getMiddec1() != null, SnmdxdjcjlbBsInfo::getMiddec1, bo.getMiddec1());
        lqw.eq(StringUtils.isNotBlank(bo.getMidmd1()), SnmdxdjcjlbBsInfo::getMidmd1, bo.getMidmd1());
        lqw.eq(bo.getMidsyzl2() != null, SnmdxdjcjlbBsInfo::getMidsyzl2, bo.getMidsyzl2());
        lqw.eq(bo.getMiddyc2() != null, SnmdxdjcjlbBsInfo::getMiddyc2, bo.getMiddyc2());
        lqw.eq(bo.getMiddec2() != null, SnmdxdjcjlbBsInfo::getMiddec2, bo.getMiddec2());
        lqw.eq(StringUtils.isNotBlank(bo.getMidmd2()), SnmdxdjcjlbBsInfo::getMidmd2, bo.getMidmd2());
        lqw.eq(StringUtils.isNotBlank(bo.getMidpjz()), SnmdxdjcjlbBsInfo::getMidpjz, bo.getMidpjz());
        lqw.eq(bo.getXidsyzl1() != null, SnmdxdjcjlbBsInfo::getXidsyzl1, bo.getXidsyzl1());
        lqw.eq(bo.getXidzl1() != null, SnmdxdjcjlbBsInfo::getXidzl1, bo.getXidzl1());
        lqw.eq(StringUtils.isNotBlank(bo.getXidbfs1()), SnmdxdjcjlbBsInfo::getXidbfs1, bo.getXidbfs1());
        lqw.eq(bo.getXidxs1() != null, SnmdxdjcjlbBsInfo::getXidxs1, bo.getXidxs1());
        lqw.eq(StringUtils.isNotBlank(bo.getXidxzbfs1()), SnmdxdjcjlbBsInfo::getXidxzbfs1, bo.getXidxzbfs1());
        lqw.eq(bo.getXidsyzl2() != null, SnmdxdjcjlbBsInfo::getXidsyzl2, bo.getXidsyzl2());
        lqw.eq(bo.getXidzl2() != null, SnmdxdjcjlbBsInfo::getXidzl2, bo.getXidzl2());
        lqw.eq(StringUtils.isNotBlank(bo.getXidbfs2()), SnmdxdjcjlbBsInfo::getXidbfs2, bo.getXidbfs2());
        lqw.eq(bo.getXidxs2() != null, SnmdxdjcjlbBsInfo::getXidxs2, bo.getXidxs2());
        lqw.eq(StringUtils.isNotBlank(bo.getXidxzbfs2()), SnmdxdjcjlbBsInfo::getXidxzbfs2, bo.getXidxzbfs2());
        lqw.eq(StringUtils.isNotBlank(bo.getXidpjz()), SnmdxdjcjlbBsInfo::getXidpjz, bo.getXidpjz());
        lqw.eq(bo.getBibmjsymdby() != null, SnmdxdjcjlbBsInfo::getBibmjsymdby, bo.getBibmjsymdby());
        lqw.eq(bo.getBibmjsyctjby() != null, SnmdxdjcjlbBsInfo::getBibmjsyctjby, bo.getBibmjsyctjby());
        lqw.eq(bo.getBibmjkxlby() != null, SnmdxdjcjlbBsInfo::getBibmjkxlby, bo.getBibmjkxlby());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjsyzlby()), SnmdxdjcjlbBsInfo::getBibmjsyzlby, bo.getBibmjsyzlby());
        lqw.eq(bo.getBibmjwdby() != null, SnmdxdjcjlbBsInfo::getBibmjwdby, bo.getBibmjwdby());
        lqw.eq(bo.getBibmjymjlsjby() != null, SnmdxdjcjlbBsInfo::getBibmjymjlsjby, bo.getBibmjymjlsjby());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjkqndby()), SnmdxdjcjlbBsInfo::getBibmjkqndby, bo.getBibmjkqndby());
        lqw.eq(bo.getBibmjbbmjby() != null, SnmdxdjcjlbBsInfo::getBibmjbbmjby, bo.getBibmjbbmjby());
        lqw.eq(bo.getBibmjsymd1() != null, SnmdxdjcjlbBsInfo::getBibmjsymd1, bo.getBibmjsymd1());
        lqw.eq(bo.getBibmjsyctj1() != null, SnmdxdjcjlbBsInfo::getBibmjsyctj1, bo.getBibmjsyctj1());
        lqw.eq(bo.getBibmjkxl1() != null, SnmdxdjcjlbBsInfo::getBibmjkxl1, bo.getBibmjkxl1());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjsyzl1()), SnmdxdjcjlbBsInfo::getBibmjsyzl1, bo.getBibmjsyzl1());
        lqw.eq(bo.getBibmjwd1() != null, SnmdxdjcjlbBsInfo::getBibmjwd1, bo.getBibmjwd1());
        lqw.eq(bo.getBibmjymjlsj1() != null, SnmdxdjcjlbBsInfo::getBibmjymjlsj1, bo.getBibmjymjlsj1());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjkqnd1()), SnmdxdjcjlbBsInfo::getBibmjkqnd1, bo.getBibmjkqnd1());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjbbmj1()), SnmdxdjcjlbBsInfo::getBibmjbbmj1, bo.getBibmjbbmj1());
        lqw.eq(bo.getBibmjsymd2() != null, SnmdxdjcjlbBsInfo::getBibmjsymd2, bo.getBibmjsymd2());
        lqw.eq(bo.getBibmjsyctj2() != null, SnmdxdjcjlbBsInfo::getBibmjsyctj2, bo.getBibmjsyctj2());
        lqw.eq(bo.getBibmjkxl2() != null, SnmdxdjcjlbBsInfo::getBibmjkxl2, bo.getBibmjkxl2());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjsyzl2()), SnmdxdjcjlbBsInfo::getBibmjsyzl2, bo.getBibmjsyzl2());
        lqw.eq(bo.getBibmjwd2() != null, SnmdxdjcjlbBsInfo::getBibmjwd2, bo.getBibmjwd2());
        lqw.eq(bo.getBibmjymjlsj2() != null, SnmdxdjcjlbBsInfo::getBibmjymjlsj2, bo.getBibmjymjlsj2());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjkqnd2()), SnmdxdjcjlbBsInfo::getBibmjkqnd2, bo.getBibmjkqnd2());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjbbmj2()), SnmdxdjcjlbBsInfo::getBibmjbbmj2, bo.getBibmjbbmj2());
        lqw.eq(StringUtils.isNotBlank(bo.getBibmjpjz()), SnmdxdjcjlbBsInfo::getBibmjpjz, bo.getBibmjpjz());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), SnmdxdjcjlbBsInfo::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SnmdxdjcjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), SnmdxdjcjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), SnmdxdjcjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), SnmdxdjcjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, SnmdxdjcjlbBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增水泥密度、细度试验检测记录
     */
    @Override
    public Boolean insertByBo(SnmdxdjcjlbBsInfoBo bo) {
        SnmdxdjcjlbBsInfo add = BeanUtil.toBean(bo, SnmdxdjcjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改水泥密度、细度试验检测记录
     */
    @Override
    public Boolean updateByBo(SnmdxdjcjlbBsInfoBo bo) {
        SnmdxdjcjlbBsInfo update = BeanUtil.toBean(bo, SnmdxdjcjlbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnmdxdjcjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥密度、细度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public SnmdxdjcjlbBsInfoVo compute(SnmdxdjcjlbBsInfoVo vo) {
        //试样1
        if (!BigDecimalUtil.isAnyOneEmpty(vo.getBibmjsymdby(),vo.getBibmjkxlby(),vo.getBibmjymjlsjby(),vo.getBibmjbbmjby(),
            vo.getBibmjsymd1(),vo.getBibmjkxl1(),vo.getBibmjymjlsj1())){
            BigDecimal sqrt1 = BigDecimal.valueOf(Math.sqrt(vo.getBibmjymjlsj1().doubleValue())).multiply(new BigDecimal(1).subtract(vo.getBibmjkxlby()));
            BigDecimal sqrt2 = BigDecimal.valueOf(Math.sqrt(Math.pow(vo.getBibmjkxl1().doubleValue(),3))).multiply(vo.getBibmjsymdby());
            BigDecimal sqrt3 = BigDecimal.valueOf(Math.sqrt(vo.getBibmjymjlsjby().doubleValue())).multiply(new BigDecimal(1).subtract(vo.getBibmjkxl1()));
            BigDecimal sqrt4 = BigDecimal.valueOf(Math.sqrt(Math.pow(vo.getBibmjkxlby().doubleValue(),3))).multiply(vo.getBibmjsymd1());
            BigDecimal multiply1 = vo.getBibmjbbmjby().multiply(sqrt1).multiply(sqrt2);
            BigDecimal multiply2 = sqrt3.multiply(sqrt4);
            vo.setBibmjbbmj1(String.valueOf(BigDecimalUtil.divide(multiply1,multiply2)));
        }
        //试样2
        if (!BigDecimalUtil.isAnyOneEmpty(vo.getBibmjsymdby(),vo.getBibmjkxlby(),vo.getBibmjymjlsjby(),vo.getBibmjbbmjby(),
            vo.getBibmjsymd2(),vo.getBibmjkxl2(),vo.getBibmjymjlsj2())){
            BigDecimal sqrt1 = BigDecimal.valueOf(Math.sqrt(vo.getBibmjymjlsj2().doubleValue())).multiply(new BigDecimal(1).subtract(vo.getBibmjkxlby()));
            BigDecimal sqrt2 = BigDecimal.valueOf(Math.sqrt(Math.pow(vo.getBibmjkxl2().doubleValue(),3))).multiply(vo.getBibmjsymdby());
            BigDecimal sqrt3 = BigDecimal.valueOf(Math.sqrt(vo.getBibmjymjlsjby().doubleValue())).multiply(new BigDecimal(1).subtract(vo.getBibmjkxl2()));
            BigDecimal sqrt4 = BigDecimal.valueOf(Math.sqrt(Math.pow(vo.getBibmjkxlby().doubleValue(),3))).multiply(vo.getBibmjsymd2());
            BigDecimal multiply1 = vo.getBibmjbbmjby().multiply(sqrt1).multiply(sqrt2);
            BigDecimal multiply2 = sqrt3.multiply(sqrt4);
            vo.setBibmjbbmj2(String.valueOf(BigDecimalUtil.divide(multiply1,multiply2)));
        }
        //平均值
        if(ObjectUtils.isNotEmpty(vo.getBibmjbbmj1())&&ObjectUtils.isNotEmpty(vo.getBibmjbbmj2())){
            BigDecimal pjz = BigDecimalUtil.divide(BigDecimalUtil.add(new BigDecimal(vo.getBibmjbbmj1()), new BigDecimal(vo.getBibmjbbmj2())),new BigDecimal(2));
            vo.setBibmjpjz(String.valueOf(pjz));
        }

        if (!BigDecimalUtil.isAnyOneEmpty(vo.getNum1())&&ObjectUtils.isNotEmpty(vo.getSymbolValue())
            &&ObjectUtils.isNotEmpty(vo.getBibmjbbmj1())&&ObjectUtils.isNotEmpty(vo.getBibmjbbmj2())){
            BigDecimal danzhi1= new BigDecimal(vo.getBibmjbbmj1());
            BigDecimal danzhi2= new BigDecimal(vo.getBibmjbbmj2());
            BigDecimal chazhi = BigDecimalUtil.isGreaterOrEqual(danzhi1,danzhi2)?BigDecimalUtil.subtract(danzhi1,danzhi2): BigDecimalUtil.subtract(danzhi2,danzhi1);
            BigDecimal shijcz = BigDecimalUtil.multiply(BigDecimalUtil.divide(chazhi, new BigDecimal(vo.getBibmjpjz()),3), new BigDecimal(100));
            vo.setShijcz(shijcz);
            List<TgsybgBaDetailVo> baDetailVos = new ArrayList<>();
            TgsybgBaDetailVo baDetailVo = new TgsybgBaDetailVo();
            baDetailVo.setQualId(84L);
            baDetailVo.setSymbolValue(vo.getSymbolValue());
            baDetailVo.setNum1(vo.getNum1());
            baDetailVo.setNum2(vo.getNum2());
            baDetailVo.setJiancjg(shijcz);
            baDetailVos.add(baDetailVo);
            baDetailVos = biQualificationService.compute(baDetailVos);
            vo.setYunxcz(baDetailVo.getJiszb());
            vo.setJiegpd(baDetailVo.getJiegpd());
        }
        return vo;
    }
}
